import scrapy

class itemSpider(scrapy.Spider):
    name = 'itemSpider'
    start_urls = ['http://lab.scrapyd.cn']

    def parse(self,response):
        # mingyan = response.css('div.quote')[0]


        # text = mingyan.css('.text::text').extract_first() # extract quote
        # author = mingyan.css('.author::text').extract_first() # extract author
        # tags = mingyan.css('.tags .tag::text').extract() #extract the tags
        # tags = ','.join(tags) # transfer the array to string
        # # next step,save to file

        # fileName = '%s-语言.txt' % author # crawl content save to file,file name is:author-text.txt
        # f = open(fileName,'a+') # add write file
        # f.write(text) # write content
        # f.write('\n') # change line
        # f.write('标签: '+tags) # wirte tags
        # f.close() # close the file oper


        mingyan = response.css('div.quote') # extract all quotes in the first page,and save to var mingyan

        for v in mingyan: # loop aquire every quotes content : quotes content,author,tags
            text = v.css('.text::text').extract_first() # extract quote
            author = v.css('.author::text').extract_first() # extract author
            tags = v.css('.tags .tag::text').extract() #extract the tags
            tags = ','.join(tags) # transfer the array to string
            # next step,save to file

            """
            next go into write file ,every personage's quote is save tp a txt file
            """
            filename = '%s-语录.txt' % author # define filename,like : 木心-语录.txt

            with open(filename,"a+") as f: # different personage 's quote is saved to different txt file,and "a+" is menas to add content
                f.write(text)
                f.write('\n') # '\n' refer to next line
                f.write('标签:' + tags)
                f.write('\n-----------------\n')
                f.close()